package com.zbf.stream的使用;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/17  15:17
 * 描述:
 */
public class TestStream {
    public static void main(String[] args) {

        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map1=new HashMap<>();
        map1.put("name","张三");
        map1.put("age",12);
        Map<String,Object> map11=new HashMap<>();
        map11.put("name","张三");
        map11.put("age",15);
        Map<String,Object> map111=new HashMap<>();
        map111.put("name","张三");
        map111.put("age",15);
        Map<String,Object> map2=new HashMap<>();
        map2.put("name","张三2");
        map2.put("age",12);
        Map<String,Object> map3=new HashMap<>();
        map3.put("name","李四");
        map3.put("age",10);
        Map<String,Object> map33=new HashMap<>();
        map33.put("name","李四");
        map33.put("age",10);
        Map<String,Object> map4=new HashMap<>();
        map4.put("name","王五");
        map4.put("age",11);

        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map33);
        list.add(map11);
        list.add(map111);

        //map数据的分组操作
       /* Map<Object, List<Map<String, Object>>> age = list.stream().collect(Collectors.groupingBy(map -> map.get("age")));
        System.out.println(age);*/

        //去重操作
        /*List<Map<String, Object>> collect = list.stream().distinct().collect(Collectors.toList());
        System.out.println(collect);*/

        //根据字段进行去除重复操作
       /* ArrayList<Map<String, Object>> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(m -> m.get("name").toString()))), ArrayList::new));

        System.out.println(collect);*/

        //聚合操作(返回任意一个最大值)
       /* Optional<Map<String, Object>> max = list.stream().max((m1, m2) -> {
            Integer integer1 = Integer.valueOf(m1.get("age").toString());
            Integer integer2 = Integer.valueOf(m2.get("age").toString());
            if ((integer1 - integer2) == 0) {
                return 0;
            } else if ((integer1 - integer2) > 0) {
                return 1;
            } else {
                return -1;
            }
        });*/

        //聚合操作(返回所有的最大值)
        Map.Entry<String, List<Map<String, Object>>> age = list.stream().collect(Collectors.groupingBy(ma -> ma.get("age").toString(), TreeMap::new, Collectors.toList())).lastEntry();
        System.out.println(age);


        //最小值
        /*Optional<Map<String, Object>> min = list.stream().min((m1, m2) -> {
            Integer integer1 = Integer.valueOf(m1.get("age").toString());
            Integer integer2 = Integer.valueOf(m2.get("age").toString());
            if ((integer1 - integer2) == 0) {
                return 0;
            } else if ((integer1 - integer2) > 0) {
                return 1;
            } else {
                return -1;
            }
        });
        System.out.println(min);*/



        Map.Entry<String, List<Map<String, Object>>> agemin = list.stream().collect(Collectors.groupingBy(ma -> ma.get("age").toString(), TreeMap::new, Collectors.toList())).firstEntry();
        System.out.println(agemin.getValue());


        //平均值
        Double age1 = list.stream().collect(Collectors.averagingDouble(map -> Integer.valueOf(map.get("age").toString())));
        System.out.println(age1);

        //求和
        Integer age2 = list.stream().collect(Collectors.summingInt(map -> Integer.valueOf(map.get("age").toString())));
        System.out.println(age2);

        //Filter的使用
        Stream<Map<String, Object>> mapStream = list.stream().filter(map -> {
            if (map.get("name").equals("张三")) {
                return true;
            }
            return false;
        });
        List<Map<String, Object>> collect = mapStream.collect(Collectors.toList());
        System.out.println(collect);

        //List转map
        Map<String, String> collect1 = list.stream().collect(Collectors.toMap(ma -> {
            return ma.get("name").toString();
        }, ma2 -> {
            return ma2.get("age").toString();
        },(k1,k2)->{return k2;}));

        System.out.println(collect1);

        //map转list
        List<Object> collect2 = map1.entrySet().stream().map(e -> {
            return e.getValue()+"_"+e.getKey();
        }).collect(Collectors.toList());

        System.out.println(collect2);


    }
}
